package com.zbkc.dialog;

import com.zbkc.main.R;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class LoadingDialog extends Dialog {

	private String backTipMsg;
	private boolean canNotCancel;
	private ImageView ivLoading;
	private AnimationDrawable loadingAnimation;
	private TextView tvLoadingLabel;

	public LoadingDialog(Context paramContext) {
		this(paramContext, null);
	}

	public LoadingDialog(Context paramContext, String paramString) {
		this(paramContext, paramString, false, null);
	}

	public LoadingDialog(Context paramContext, String paramString1,
			boolean paramBoolean, String paramString2) {
		super(paramContext);
		canNotCancel = paramBoolean;
		backTipMsg = paramString2;
		getContext().setTheme(16973908);
		setContentView(R.layout.loading_dialog);
		ivLoading = ((ImageView) findViewById(R.id.iv_loading));
		tvLoadingLabel = ((TextView) findViewById(R.id.tv_loading_label));
		if (!TextUtils.isEmpty(paramString1))
			setLoadingMessage(paramString1);
		Window localWindow = getWindow();
		WindowManager.LayoutParams localLayoutParams = localWindow
				.getAttributes();
		localLayoutParams.flags = 2;
		localLayoutParams.dimAmount = 0.5F;
		localWindow.setLayout(-2, -2);
	}

	public void dismiss() {
		loadingAnimation.stop();
		super.dismiss();
	}

	public boolean onKeyDown(int paramInt, KeyEvent paramKeyEvent) {
		if ((paramInt == 4) && (this.canNotCancel)) {
			Toast.makeText(getContext(), this.backTipMsg, 0).show();
			return true;
		}
		return super.onKeyDown(paramInt, paramKeyEvent);
	}

	public void onWindowFocusChanged(boolean paramBoolean) {
		super.onWindowFocusChanged(paramBoolean);
		if (paramBoolean) {
			ivLoading.setBackgroundResource(2130968576);
			loadingAnimation = ((AnimationDrawable) this.ivLoading
					.getBackground());
			loadingAnimation.start();
		}
	}

	public void setLoadingMessage(String paramString) {
		if (paramString != null)
			tvLoadingLabel.setText(paramString);
	}

}
